package com.liuzhongni.wahahanews.singleton;

import android.content.Context;
import android.util.Log;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.liuzhongni.wahahanews.R;
import com.liuzhongni.wahahanews.activity.MainActivity;
import com.liuzhongni.wahahanews.dao.DaoMaster;
import com.liuzhongni.wahahanews.dao.DaoSession;
import com.liuzhongni.wahahanews.dao.Person;
import com.liuzhongni.wahahanews.dao.PersonDao;
import com.liuzhongni.wahahanews.headlineclass.DoubleCache;
import com.liuzhongni.wahahanews.headlineclass.MyDiskCache;
import com.liuzhongni.wahahanews.headlineclass.MyMemoryCache;

import java.util.ArrayList;

import de.greenrobot.dao.query.QueryBuilder;

/**
 * Created by dllo on 16/1/13.
 */
public class VolleySingleton extends Volley{
    //volatile 新关键字 更好的处理线程对象
    private  RequestQueue queue;
    private  ImageLoader imageLoader;
    private  DoubleCache doubleCache;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private Context context;


//    public static VolleySingleton getInstance() {
//        if(ourInstance == null){
//            synchronized (VolleySingleton.class){
//                if (ourInstance == null) {
//
//                    //线程锁 同时只有一个线程可以调用
//                    ourInstance = new VolleySingleton();
//                }
//            }
//        }
//        return ourInstance;
//    }

    private static class SingleHolder {
        private static final VolleySingleton ourInstance = new VolleySingleton();
//        private static final MyMemoryCache cache = new MyMemoryCache();
    }

    public static VolleySingleton getOurInstance() {
        return SingleHolder.ourInstance;
    }


    private VolleySingleton() {
        queue = getQueue();
        doubleCache = getCache();
        imageLoader = getImageLoader();

    }

    public RequestQueue getQueue() {
        Log.d("VolleySingleton", "44444444"+queue);
        if (queue == null) {
            queue = Volley.newRequestQueue(BaseSingleton.getContext());
        }
        return queue;
    }

    public DoubleCache getCache() {
        Log.d("VolleySingleton", "22222222");
        if (doubleCache == null) {
            doubleCache = new DoubleCache();
        }
        return doubleCache;
    }

    public ImageLoader getImageLoader() {
        Log.d("VolleySingleton", "33333333");
        if (imageLoader == null) {
            imageLoader = new ImageLoader(getQueue(), doubleCache);
        }

        return imageLoader;
    }

    public  void addRequest (StringRequest request){
        getOurInstance().getQueue().add(request);
    }


//    public static void addRequest(Request request) {
//
//        getQueue().add(request);
//    }

//    public static class MyMemoryCache implements ImageLoader.ImageCache {
//
//        private LruCache<String,Bitmap> cache = new LruCache<>(1000*1024*1024/4);
//        @Override
//        public Bitmap getBitmap(String url) {
//            return cache.get(url);
//        }
//
//        @Override
//        public void putBitmap(String url, Bitmap bitmap) {
//            cache.put(url,bitmap);
//        }
//    }

    public  void imgJson(NetworkImageView networkImageView, String url) {
        networkImageView.setDefaultImageResId(R.mipmap.iconfont_jinxingzhongnew);
        networkImageView.setErrorImageResId(R.mipmap.iconfont_jiazaishibainew);
        networkImageView.setImageUrl(url, imageLoader);
    }

//    public void putInResponse (String name,String response){
//        MainActivity.daoSession.getPersonDao().insertOrReplace(new Person(name, response));
//
//    }
//    public String getOutResponse(String name){
//        QueryBuilder queryBuilder = daoSession.getPersonDao().queryBuilder();
//        queryBuilder.where(PersonDao.Properties.Name.eq(name));0
//        ArrayList<String> data = (ArrayList<String>) queryBuilder.list();
//        return data.get(0);
//    }


}
